home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
util
/
cryptography.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
6KB
|
215 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from hashlib import sha1 as _DefaultHash
def _key_prep(key, hash = _DefaultHash, length = 16):
return hash(key).digest()[:length]
def pad(s, sz, padchar = '\x00'):
extralen = (sz - len(s) % sz) % sz
return s + extralen * padchar
def unpad(s, padchar = '\x00'):
return s.rstrip(padchar)
def _encrypt(key, plain, padchar = '\x00'):
Cipher = AES
import Crypto.Cipher
cipher = Cipher.new(key)
plain = pad(plain, cipher.block_size, padchar)
return cipher.encrypt(plain)
def _decrypt(key, crypt, padchar = '\x00'):
Cipher = AES
import Crypto.Cipher
cipher = Cipher.new(key)
return cipher.decrypt(crypt).rstrip(padchar)
def cipher_functions(key, padchar = '\x00'):
def _encrypt(plain):
return encrypt(key, plain, padchar = padchar)
def _decrypt(crypt):
return decrypt(key, crypt, padchar = padchar)
return (_encrypt, _decrypt)
def hash(s, raw = True, Hash = _DefaultHash):
if raw:
digest = lambda x: x.digest()
else:
digest = lambda x: x.hexdigest()
return digest(Hash(s))
def encrypt(key, plain, iv = None, padchar = '\x00'):
if iv is None:
iv = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
mode = 2
aes = OpenSSL_AES(key, mode, iv)
plain = pad(plain, aes.block_size, padchar)
return aes.encrypt(plain)
def decrypt(key, crypt, iv = None, padchar = '\x00'):
if iv is None:
iv = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
mode = 2
aes = OpenSSL_AES(key, mode, iv)
return unpad(aes.decrypt(crypt), padchar)
try:
from M2Crypto import m2
except ImportError:
has_AES = False
has_DES3 = False
has_AES = True
has_DES3 = True
class OpenSSL_AES(object):
def __init__(self, key, mode, IV):
if len(key) not in (16, 24, 32):
raise AssertionError()
if mode != 2:
raise AssertionError()
if len(IV) != 16:
raise AssertionError()
self.isBlockCipher = True
self.block_size = 16
self.implementation = 'openssl'
if len(key) == 16:
self.name = 'aes128'
elif len(key) == 24:
self.name = 'aes192'
elif len(key) == 32:
self.name = 'aes256'
else:
raise AssertionError()
self.key = key
self.IV = IV
def _createContext(self, encrypt):
context = m2.cipher_ctx_new()
if len(self.key) == 16:
cipherType = m2.aes_128_ecb()
if len(self.key) == 24:
cipherType = m2.aes_192_ecb()
if len(self.key) == 32:
cipherType = m2.aes_256_ecb()
m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
return context
def encrypt(self, plaintext):
if not plaintext:
return ''
context = self._createContext(1)
ciphertext = m2.cipher_update(context, plaintext)
m2.cipher_ctx_free(context)
self.IV = ciphertext[-(self.block_size):]
return ciphertext
def decrypt(self, ciphertext):
if not ciphertext:
return ''
context = self._createContext(0)
plaintext = m2.cipher_update(context, ciphertext + '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
plaintext = plaintext[:len(ciphertext)]
m2.cipher_ctx_free(context)
self.IV = ciphertext[-(self.block_size):]
return plaintext
AES = OpenSSL_AES
class OpenSSL_TripleDES(object):
def __init__(self, key, mode, IV):
if len(key) != 24:
raise ValueError()
if mode != 2:
raise ValueError()
if len(IV) != 8:
raise ValueError()
self.isBlockCipher = True
self.block_size = 8
self.implementation = 'openssl'
self.name = '3des'
self.key = key
self.IV = IV
def _createContext(self, encrypt):
context = m2.cipher_ctx_new()
cipherType = m2.des_ede3_cbc()
m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
return context
def encrypt(self, plaintext):
if not plaintext:
return ''
context = self._createContext(1)
ciphertext = m2.cipher_update(context, plaintext)
m2.cipher_ctx_free(context)
self.IV = ciphertext[-(self.block_size):]
return ciphertext
def decrypt(self, ciphertext):
if not ciphertext:
return ''
context = self._createContext(0)
plaintext = m2.cipher_update(context, ciphertext + '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
plaintext = plaintext[:len(ciphertext)]
m2.cipher_ctx_free(context)
self.IV = ciphertext[-(self.block_size):]
return plaintext
DES3 = OpenSSL_TripleDES
def _main():
key = _key_prep('this is my key')
plaintext = 'abcd' * 80
old = encrypt(key, plaintext)
new = _encrypt(key, plaintext)
print (old, new), old == new
print (decrypt(key, new), _decrypt(key, old))
print 'yay'
if __name__ == '__main__':
_main()